package org.alwayssuper.protocol.commons.transform.realtimev2025;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 2025版报警数据格式 (0x06)
 * 参考GB/T 32960.3-2025标准表23-24
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class AlarmData {
    @Field(length = 1, desc = "最高报警等级:0:无故障;1:1级故障;2:2级故障;3:3级故障;4:热事件故障")
    private int maxAlarmLevel;
    
    @Field(length = 4, desc = "通用报警标志:通用报警标志位定义见表24")
    private int generalAlarmFlags;
    
    // 可充电储能装置故障总数N1:0~253(由batteryFaultCodes的totalUnit获取)
    private int batteryFaultCount;
    
    @Field(totalUnit = 1, desc = "可充电储能装置故障代码列表")
    private int[] batteryFaultCodes;
    
    // 驱动电机故障总数N2:0~253(由motorFaultCodes的totalUnit获取)
    private int motorFaultCount;
    
    @Field(totalUnit = 1, desc = "驱动电机故障代码列表")
    private int[] motorFaultCodes;
    
    // 发动机故障总数N3:0~253(由engineFaultCodes的totalUnit获取)
    private int engineFaultCount;
    
    @Field(totalUnit = 1, desc = "发动机故障代码列表")
    private int[] engineFaultCodes;
    
    // 其他故障总数N4:0~253(由otherFaultCodes的totalUnit获取)
    private int otherFaultCount;
    
    @Field(totalUnit = 1, desc = "其他故障代码列表")
    private int[] otherFaultCodes;
    
    // 通用报警故障总数N5:0~253(由generalAlarmFaultLevels的totalUnit获取)
    private int generalAlarmFaultCount;
    
    @Field(totalUnit = 1, desc = "通用报警故障等级列表")
    private List<GeneralAlarmFaultLevel> generalAlarmFaultLevels;
    
    @Data
    @Accessors(chain = true)
    public static class GeneralAlarmFaultLevel {
        @Field(length = 1, desc = "通用报警故障位序号:0~31")
        private int bitPosition;
        
        @Field(length = 1, desc = "通用报警故障等级:0~4")
        private int faultLevel;
    }
    
    // 2025版通用报警标志位定义常量 - 基于GB/T32960.3-2025表24扩展
    public static final int TEMP_DIFF_ALARM = 0;                       // 位0: 温度差异报警
    public static final int BATTERY_HIGH_TEMP_ALARM = 1;               // 位1: 电池高温报警
    public static final int BATTERY_OVERVOLTAGE_ALARM = 2;              // 位2: 车载储能装置类型过压报警
    public static final int BATTERY_UNDERVOLTAGE_ALARM = 3;             // 位3: 车载储能装置类型欠压报警
    public static final int SOC_LOW_ALARM = 4;                          // 位4: SOC低报警
    public static final int PARALLEL_UNIT_OVERVOLTAGE_ALARM = 5;        // 位5: 最小并联单元过压报警(2025版更新)
    public static final int PARALLEL_UNIT_UNDERVOLTAGE_ALARM = 6;       // 位6: 最小并联单元欠压报警(2025版更新)
    public static final int SOC_HIGH_ALARM = 7;                         // 位7: SOC过高报警
    public static final int SOC_JUMP_ALARM = 8;                         // 位8: SOC跳变报警
    public static final int ENERGY_STORAGE_MISMATCH_ALARM = 9;          // 位9: 可充电储能系统不匹配报警
    public static final int PARALLEL_UNIT_CONSISTENCY_ALARM = 10;       // 位10: 最小并联单元一致性差报警(2025版更新)
    public static final int INSULATION_RESISTANCE_FAILURE_ALARM = 11;   // 位11: 绝缘电阻失效报警(2025版更新)
    public static final int DCDC_TEMP_ALARM = 12;                       // 位12: DC-DC温度报警
    public static final int BRAKE_SYSTEM_ALARM = 13;                    // 位13: 制动系统报警
    public static final int DCDC_STATUS_ALARM = 14;                     // 位14: DC-DC状态报警
    public static final int MOTOR_CONTROLLER_TEMP_ALARM = 15;           // 位15: 驱动电机控制器温度报警
    public static final int HIGH_VOLTAGE_INTERLOCK_ALARM = 16;          // 位16: 高压互锁状态报警
    public static final int MOTOR_TEMP_ALARM = 17;                      // 位17: 驱动电机温度报警
    public static final int BATTERY_OVERCHARGE_ALARM = 18;              // 位18: 车载储能装置类型过充
    public static final int MOTOR_OVERSPEED_ALARM = 19;                 // 位19: 驱动电机超速报警(2025版新增)
    public static final int MOTOR_OVERCURRENT_ALARM = 20;               // 位20: 驱动电机过流报警(2025版新增)
    public static final int SUPERCAPACITOR_OVERTEMP_ALARM = 21;         // 位21: 超级电容过温报警(2025版新增)
    public static final int SUPERCAPACITOR_OVERVOLTAGE_ALARM = 22;      // 位22: 超级电容过压报警(2025版新增)
    public static final int BATTERY_THERMAL_EVENT_ALARM = 23;           // 位23: 可充电储能装置热事件报警(2025版新增)
    public static final int HYDROGEN_LEAK_ALARM = 24;                   // 位24: 氢气泄漏异常报警(2025版新增)
    public static final int HYDROGEN_SYSTEM_PRESSURE_ALARM = 25;        // 位25: 车载氢系统压力异常报警(2025版新增)
    public static final int HYDROGEN_SYSTEM_TEMP_ALARM = 26;            // 位26: 车载氢系统温度异常报警(2025版新增)
    public static final int FUEL_CELL_STACK_OVERTEMP_ALARM = 27;        // 位27: 燃料电池电堆超温报警(2025版新增)
    // 位28~31: 预留
    
    // 通过各个故障代码列表长度返回对应的故障总数
    public int getBatteryFaultCount() {
        return batteryFaultCodes == null ? 0 : batteryFaultCodes.length;
    }
    
    public int getMotorFaultCount() {
        return motorFaultCodes == null ? 0 : motorFaultCodes.length;
    }
    
    public int getEngineFaultCount() {
        return engineFaultCodes == null ? 0 : engineFaultCodes.length;
    }

    public int getOtherFaultCount() {
        return otherFaultCodes == null ? 0 : otherFaultCodes.length;
    }
    
    public int getGeneralAlarmFaultCount() {
        return generalAlarmFaultLevels == null ? 0 : generalAlarmFaultLevels.size();
    }
    
    /**
     * 检查指定位的报警状态
     * @param bitPosition 位位置 (0-31)
     * @return true: 报警, false: 正常
     */
    public boolean isAlarmBitSet(int bitPosition) {
        if (bitPosition < 0 || bitPosition > 31) {
            throw new IllegalArgumentException("位位置必须在0-31范围内");
        }
        return (generalAlarmFlags & (1 << bitPosition)) != 0;
    }

    /**
     * 设置指定位的报警状态
     * @param bitPosition 位位置 (0-31)
     * @param alarm true: 设置报警, false: 清除报警
     */
    public void setAlarmBit(int bitPosition, boolean alarm) {
        if (bitPosition < 0 || bitPosition > 31) {
            throw new IllegalArgumentException("位位置必须在0-31范围内");
        }
        if (alarm) {
            generalAlarmFlags |= (1 << bitPosition);
        } else {
            generalAlarmFlags &= ~(1 << bitPosition);
        }
    }

    // 便捷方法：检查具体报警类型(2025版)
    public boolean isTempDiffAlarm() { return isAlarmBitSet(TEMP_DIFF_ALARM); }
    public boolean isBatteryHighTempAlarm() { return isAlarmBitSet(BATTERY_HIGH_TEMP_ALARM); }
    public boolean isBatteryOvervoltageAlarm() { return isAlarmBitSet(BATTERY_OVERVOLTAGE_ALARM); }
    public boolean isBatteryUndervoltageAlarm() { return isAlarmBitSet(BATTERY_UNDERVOLTAGE_ALARM); }
    public boolean isSocLowAlarm() { return isAlarmBitSet(SOC_LOW_ALARM); }
    public boolean isParallelUnitOvervoltageAlarm() { return isAlarmBitSet(PARALLEL_UNIT_OVERVOLTAGE_ALARM); }
    public boolean isParallelUnitUndervoltageAlarm() { return isAlarmBitSet(PARALLEL_UNIT_UNDERVOLTAGE_ALARM); }
    public boolean isSocHighAlarm() { return isAlarmBitSet(SOC_HIGH_ALARM); }
    public boolean isSocJumpAlarm() { return isAlarmBitSet(SOC_JUMP_ALARM); }
    public boolean isEnergyStorageMismatchAlarm() { return isAlarmBitSet(ENERGY_STORAGE_MISMATCH_ALARM); }
    public boolean isParallelUnitConsistencyAlarm() { return isAlarmBitSet(PARALLEL_UNIT_CONSISTENCY_ALARM); }
    public boolean isInsulationResistanceFailureAlarm() { return isAlarmBitSet(INSULATION_RESISTANCE_FAILURE_ALARM); }
    public boolean isDcdcTempAlarm() { return isAlarmBitSet(DCDC_TEMP_ALARM); }
    public boolean isBrakeSystemAlarm() { return isAlarmBitSet(BRAKE_SYSTEM_ALARM); }
    public boolean isDcdcStatusAlarm() { return isAlarmBitSet(DCDC_STATUS_ALARM); }
    public boolean isMotorControllerTempAlarm() { return isAlarmBitSet(MOTOR_CONTROLLER_TEMP_ALARM); }
    public boolean isHighVoltageInterlockAlarm() { return isAlarmBitSet(HIGH_VOLTAGE_INTERLOCK_ALARM); }
    public boolean isMotorTempAlarm() { return isAlarmBitSet(MOTOR_TEMP_ALARM); }
    public boolean isBatteryOverchargeAlarm() { return isAlarmBitSet(BATTERY_OVERCHARGE_ALARM); }
    // 2025版新增报警类型便捷方法
    public boolean isMotorOverspeedAlarm() { return isAlarmBitSet(MOTOR_OVERSPEED_ALARM); }
    public boolean isMotorOvercurrentAlarm() { return isAlarmBitSet(MOTOR_OVERCURRENT_ALARM); }
    public boolean isSupercapacitorOvertempAlarm() { return isAlarmBitSet(SUPERCAPACITOR_OVERTEMP_ALARM); }
    public boolean isSupercapacitorOvervoltageAlarm() { return isAlarmBitSet(SUPERCAPACITOR_OVERVOLTAGE_ALARM); }
    public boolean isBatteryThermalEventAlarm() { return isAlarmBitSet(BATTERY_THERMAL_EVENT_ALARM); }
    public boolean isHydrogenLeakAlarm() { return isAlarmBitSet(HYDROGEN_LEAK_ALARM); }
    public boolean isHydrogenSystemPressureAlarm() { return isAlarmBitSet(HYDROGEN_SYSTEM_PRESSURE_ALARM); }
    public boolean isHydrogenSystemTempAlarm() { return isAlarmBitSet(HYDROGEN_SYSTEM_TEMP_ALARM); }
    public boolean isFuelCellStackOvertempAlarm() { return isAlarmBitSet(FUEL_CELL_STACK_OVERTEMP_ALARM); }

    /**
     * 清除所有报警标志
     */
    public void clearAllAlarms() {
        generalAlarmFlags = 0;
    }

    /**
     * 检查是否有任何报警
     * @return true: 有报警, false: 无报警
     */
    public boolean hasAnyAlarm() {
        return generalAlarmFlags != 0;
    }

    /**
     * 获取报警标志的十六进制字符串表示
     * @return 十六进制字符串
     */
    public String getAlarmHexString() {
        return String.format("0x%08X", generalAlarmFlags);
    }

    /**
     * 获取当前所有激活报警的描述(2025版)
     * @return 报警描述列表
     */
    public java.util.List<String> getActiveAlarmDescriptions() {
        java.util.List<String> alarms = new java.util.ArrayList<>();
        if (isTempDiffAlarm()) alarms.add("温度差异报警");
        if (isBatteryHighTempAlarm()) alarms.add("电池高温报警");
        if (isBatteryOvervoltageAlarm()) alarms.add("车载储能装置类型过压报警");
        if (isBatteryUndervoltageAlarm()) alarms.add("车载储能装置类型欠压报警");
        if (isSocLowAlarm()) alarms.add("SOC低报警");
        if (isParallelUnitOvervoltageAlarm()) alarms.add("最小并联单元过压报警");
        if (isParallelUnitUndervoltageAlarm()) alarms.add("最小并联单元欠压报警");
        if (isSocHighAlarm()) alarms.add("SOC过高报警");
        if (isSocJumpAlarm()) alarms.add("SOC跳变报警");
        if (isEnergyStorageMismatchAlarm()) alarms.add("可充电储能系统不匹配报警");
        if (isParallelUnitConsistencyAlarm()) alarms.add("最小并联单元一致性差报警");
        if (isInsulationResistanceFailureAlarm()) alarms.add("绝缘电阻失效报警");
        if (isDcdcTempAlarm()) alarms.add("DC-DC温度报警");
        if (isBrakeSystemAlarm()) alarms.add("制动系统报警");
        if (isDcdcStatusAlarm()) alarms.add("DC-DC状态报警");
        if (isMotorControllerTempAlarm()) alarms.add("驱动电机控制器温度报警");
        if (isHighVoltageInterlockAlarm()) alarms.add("高压互锁状态报警");
        if (isMotorTempAlarm()) alarms.add("驱动电机温度报警");
        if (isBatteryOverchargeAlarm()) alarms.add("车载储能装置类型过充");
        // 2025版新增报警
        if (isMotorOverspeedAlarm()) alarms.add("驱动电机超速报警");
        if (isMotorOvercurrentAlarm()) alarms.add("驱动电机过流报警");
        if (isSupercapacitorOvertempAlarm()) alarms.add("超级电容过温报警");
        if (isSupercapacitorOvervoltageAlarm()) alarms.add("超级电容过压报警");
        if (isBatteryThermalEventAlarm()) alarms.add("可充电储能装置热事件报警");
        if (isHydrogenLeakAlarm()) alarms.add("氢气泄漏异常报警");
        if (isHydrogenSystemPressureAlarm()) alarms.add("车载氢系统压力异常报警");
        if (isHydrogenSystemTempAlarm()) alarms.add("车载氢系统温度异常报警");
        if (isFuelCellStackOvertempAlarm()) alarms.add("燃料电池电堆超温报警");
        return alarms;
    }
}